package org.yjw;

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;

/**
 * commons.io包下  FileUtils工具类的
 * 建议用这个
 */
public class CommonsIoFileUtilsTest {

    public static void main(String[] args) {
        CommonsIoFileUtilsTest test = new CommonsIoFileUtilsTest();

        //电脑d盘中的baoming.png 图片的路径
        String filePath1 = "D:/baoming.png";

        //电脑d盘中的baoming2.png 图片的路径
        String filePath2 = "D:/baoming2.png";

        //复制文件
        try {
            test.copyFile2();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 复制文件夹
     */
    public void copyFile() {
        String filePath1 = "F:/dd";
        File file1 = new File(filePath1);

        String filePath2 = "F:/ee";
        File file2 = new File(filePath2);

        try {
            //复制文件夹
            FileUtils.copyDirectory(file1, file2);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 复制文件
     */
    public void copyFile2() throws IOException {
        String filePath1 = "D:/baoming.png";
        String filePath2 = "D:/baoming3.png";

        File file1 = new File(filePath1);
        File file2 = new File(filePath2);
        //复制文件
        FileUtils.copyFile(file1, file2);
    }

    /**
     * 下载远程图片
     */
    public void dowload() {
        String url = "http://imgsrc.baidu.com/baike/pic/item/7aec54e736d12f2ee289bffe4cc2d5628435689b.jpg";
        String filePath2 = "F:/abc.jpg";
        File file2 = new File(filePath2);
        try {
            //把服务器上图片下载到本地F盘的abc.jpg图片
            FileUtils.copyURLToFile(new URL(url), file2);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 写入文案到本地
     */
    public void write() {
        String mes = "下班了";
        String filePath2 = "F:/abc.xml";
        File file2 = new File(filePath2);
        try {
            FileUtils.writeStringToFile(file2, mes, "UTF-8", true);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 写入字节数组到本地
     */
    public void writeByte() {
        String mes = "哈哈，下班了";
        String filePath2 = "F:/abc.txt";
        File file2 = new File(filePath2);
        try {
            byte[] mesArray = mes.getBytes();
            FileUtils.writeByteArrayToFile(file2, mesArray);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 写入集合内容到本地
     */
    public void writeCollection() {
        ArrayList<String> list = new ArrayList<>();
        String mes = "哈哈，下班了";
        String mes2 = "回家，回家";
        list.add(mes);  //第一行数据
        list.add(mes2); //第二行数据
        String filePath2 = "F:/abc.txt";
        File file2 = new File(filePath2);
        try {
            FileUtils.writeLines(file2, list);
            //每一行以什么结尾
            FileUtils.writeLines(file2, list, "。");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 向文件中写内容
     */
    public void addWrite() {
        String mes = "哈哈，下班了";

        String filePath2 = "F:/abc.txt";
        File file2 = new File(filePath2);
        try {
            FileUtils.write(file2, mes, "utf-8", true);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
